package com.pyue.eam.regi.controller;

import cn.hutool.core.collection.CollUtil;
import com.pyue.eam.admin.api.entity.regi.CqRegiLibEntity;
import com.pyue.eam.admin.utils.EamUtils;
import com.pyue.eam.app.api.entity.AppUser;
import com.pyue.eam.common.core.util.R;
import com.pyue.eam.common.security.util.SecurityUtils;
import com.pyue.eam.regi.bo.CqRegiUserEntity;
import com.pyue.eam.regi.service.CqRegiLibService;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 团长和用户绑定表
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/cqRegiUser")
@Tag(description = "cqRegiUser", name = "团长和用户绑定")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CqRegiUserController {
    private final CqRegiLibService cqRegiLibService;

    /**
     * 团队成员列表
     */
    @GetMapping({"/memberList/{regiId}", "/memberList"})
    public R<List<CqRegiUserEntity>> memberList(@PathVariable(value = "regiId", required = false) Long regiId) {
        return R.okList(cqRegiLibService.memberList(regiId));
    }

    /**
     * 未入团成员列表
     */
    @GetMapping({"/dealMemberList/{regiId}", "/dealMemberList"})
    public R<List<CqRegiUserEntity>> dealMemberList(@PathVariable(value = "regiId", required = false) Long regiId) {
        return R.okList(cqRegiLibService.dealMemberList(regiId));
    }

    /**
     * 批量加入用户到团队
     */
    @GetMapping("/batchJoinUserList/{userIds}")
    public R batchJoinUserList(@PathVariable String userIds) {
        List<Long> userIdList = EamUtils.strSplitToLongList(userIds);
        return R.ok(cqRegiLibService.batchJoinUserList(CollUtil.distinct(userIdList)));
    }


    /**
     * 单个员工信息
     */
    @GetMapping("/getRegiUserInfo/{userId}")
    public R<AppUser> getRegiUserInfo(@PathVariable(value = "userId") Long userId) {
        return R.ok(cqRegiLibService.regiUserInfo(userId));
    }

    /**
     * 更新单个成员信息
     */
    @PutMapping("/updateRegiUserInfo")
    public R<Boolean> updateRegiUserInfo(@RequestBody AppUser appUser) {
        return R.ok(cqRegiLibService.updateRegiUserInfo(appUser));
    }

    /**
     * 查看职工绑定的团长
     */
    @GetMapping({"/getRegiWorker/{userId}", "/getRegiWorker"})
    public R<List<CqRegiLibEntity>> getRegiWorker(@PathVariable(value = "userId", required = false) Long userId) {
        userId = userId == null ? SecurityUtils.getUserId() : userId;
        return R.okList(cqRegiLibService.regiListByWorkId(userId));
    }

    /**
     * 查看职工未绑定的团长
     */
    @GetMapping({"/getDealRegiWorker/{userId}", "/getDealRegiWorker"})
    public R<List<CqRegiLibEntity>> getDealRegiWorker(@PathVariable(value = "userId", required = false) Long userId) {
        userId = userId == null ? SecurityUtils.getUserId() : userId;
        return R.okList(cqRegiLibService.dealRegiListByWorkId(userId));
    }


    /**
     * 职工批量主动加入团
     */
    @GetMapping("/batchJoinRegiList/{regiIds}")
    public R batchJoinRegiList(@PathVariable String regiIds) {
        List<Long> regiIdList = EamUtils.strSplitToLongList(regiIds);
        return R.ok(cqRegiLibService.batchJoinRegiList(SecurityUtils.getUserId(), CollUtil.distinct(regiIdList)));
    }

}

